<?php
namespace app\admin\service;

use library\tools\Node;
use think\Db;
use think\facade\Request;
use app\admin\service\MemberService;
use app\admin\service\CompanyService;
use app\admin\service\PointService;

/**
 * 系统档案管理服务
 * Class ArchivesService
 * @package app\admin\service
 */
class ArchivesService
{
	 /**
     * 添加档案
     * @param number $id  增员表id
     * @return boolen
     */
    public static function archivesAdd($id)
    {
    	$increase = Db::name('system_increase')->where(['id'=>$id,'archives'=>1])->find();
		if(!$increase){
			return false;
		}
		$arr = [
			'code' => number_rule_max(13),
			'person_id' => $increase['person_id'],
			'company_id' => $increase['company_id'],
			'contract_id' => $increase['contract_id'],
			'csr_id' => $increase['csr_id'],
			'name' => $increase['name'],
			'mobile' => $increase['mobile'],
			'id_number' => $increase['id_number']			
		];
		$re = Db::name('system_archives')->insert($arr);
		if($re){
			save_number_rule_max(13);
		}
    }
	
	
	/**
     * 转入提交，检测次数或者点数
     * @param array $id
     * @return array
     */
    public static function archives_check($id)
	{
		$archives = Db::name('system_archives')->where(['id'=>$id])->find();
		//获取档案服务id
		$service = Db::name('system_service')->where(['is_special'=>3])->find();
		//找到主公司
		$master = CompanyService::getMaster($archives['company_id']);
		$info = Db::name('system_member_count')->where(['member_id'=>$master['mid'],'service_id'=>$service['id'],'is_deleted'=>0])->find();
		if(!$info || $info['surplus'] <= 0){
			//没有次数，查找点数
			if($service['point'] > $master['point']){
				return false;
			}else{
				return 1;
			}
		}else{
			return 2;
		}		
	}

	
	
	 /**
     * 导出档案
     * @param array $id
     * @return array
     */
    public static function export_archives($id)
    {
    	$filename = '档案列表';
    	$titles = array(
    		array('code','档案编号'),
            array('name','姓名'),
            array('id_number','身份证号'),
            array('mobile','电话号码'),
            array('company_name','公司名称'),
            array('into_date','转入日期'),
            array('out_date','转出日期'),
            array('quit_date','离职/终止日期'),
            array('status','档案状态'),
            array('person_status','员工状态'),
            array('csr_id','所属客服'),
        );
    	$list = Db::name('system_archives')->where(['id'=>$id])->order('id desc')->select();
		if(!empty($list) && $list){
			$companys = Db::name('system_company')->column('name','id');
			foreach($list as $k=>$v){
	    		$arr[$k]['code'] = $v['code'];
	    		$arr[$k]['name'] = $v['name'];
				$arr[$k]['id_number'] = ' '.$v['id_number'];
				$arr[$k]['mobile'] = $v['mobile'];
	    		$arr[$k]['company_name'] = $companys[$v['company_id']];
				$arr[$k]['into_date'] = $v['into_date'];
				$arr[$k]['out_date'] = $v['out_date'];
	    		$arr[$k]['quit_date'] = $v['quit_date'];
				$arr[$k]['status'] = ['1'=>'未转入 ','2'=>'已转入','3'=>'已转出'][$v['status']];
				$arr[$k]['person_status'] = ['1'=>'在职 ','2'=>'终止','3'=>'离职'][$v['person_status']];
				$arr[$k]['csr_id'] = $v['csr_id'] ? user_list()[$v['csr_id']] : '';
	    	}
		}else{
			$arr = [];
		}   
    	
        return ['filename'=>$filename,'titles'=>$titles,'list'=>$arr];
    }

}
